import { Injectable, Inject } from '@angular/core';

import { GLOBAL_I18N } from '../keys';
import { LoggerService } from '../service/logger.service';

@Injectable({
  providedIn: 'root'
})
export class I18nService {

  private groupMap: any = {};

  constructor(
    @Inject(GLOBAL_I18N) private globalI18n: any,
    private loggerService: LoggerService
  ) {
    this.setGroupMap('core', this.globalI18n);
  }

  setGroupMap(groupKey: string, group: any) {
    if (groupKey === 'core' && this.groupMap['core']) {
      this.loggerService.error(new Error('core i18n group has exists'), 'I18nService');
    }
    this.groupMap[groupKey] = group;
  }

  getValue(itemKey: string, groupKey?) {
    if (groupKey) {
      return this.groupMap[groupKey][itemKey] || '';
    } else if (this.groupMap.hasOwnProperty('global')) {
      return this.groupMap.global[itemKey] || '';
    } else if (this.groupMap.hasOwnProperty('core')) {
      return this.groupMap.core[itemKey] || '';
    }

    return '';
  }
}
